package org.zttc.itat.action;

import java.lang.reflect.InvocationTargetException;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.zttc.itat.model.Pager;
import org.zttc.itat.model.User;
import org.zttc.itat.service.IUserService;
import org.zttc.itat.util.ActionUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	private String con;
	@Resource(name="userService")
	private IUserService userService;

	@Override
	public User getModel() {
		if(user==null) user = new User();
		return user;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public String getCon() {
		return con;
	}

	public void setCon(String con) {
		this.con = con;
	}

	/***
	 * 用户展示列表
	 * @author zxit
	 * */
	public String list(){
		System.out.println(con);
		Pager<User> pages =userService.findUser(con);
		ActionContext.getContext().put("pages",pages);
		return SUCCESS;
	}
	/***
	 * 用户添加
	 * @author zxit
	 * */
	public String add(){
		userService.add(user);
		ActionUtil.setUrl("/later_user_list.action");
		return ActionUtil.REDIRECT;
	}
	
	public void validateAdd() {
		if(ActionUtil.isEmpty(user.getUsername())) {
			this.addFieldError("username","用户名称不能为空");
		}
		if(this.hasFieldErrors()) addInput();
	}
	
	/***
	 * 进入用户添加界面
	 * @author zxit
	 * */
	public String addInput(){
		
		return SUCCESS;
	}
	
	/**
	 * 进入用户的修改界面
	 * @author zxit
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * */
	public String updateInput(){
        user = userService.load(user.getId());
		return SUCCESS;
	}
	
	/**
	 * 用户修改
	 * @author zxit
	 * */
	public String update(){
		User u = userService.load(user.getId());
		u.setEmail(user.getEmail());
		u.setNickname(user.getNickname());
		userService.update(u);
		ActionUtil.setUrl("/later_user_list.action");
		return ActionUtil.REDIRECT;
	}
	
	/**
	 * 用户的删除
	 * @author zxit
	 * **/
	public String delete(){
		userService.delete(user.getId());
		ActionUtil.setUrl("/later_user_list.action");
		return ActionUtil.REDIRECT;
	}
	
	/**
	 * 用户信息的展示
	 * @author zxit
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * */
	public String show() throws IllegalAccessException, InvocationTargetException{
		User tu = userService.load(user.getId());
		BeanUtils.copyProperties(user, tu);
		return SUCCESS;
	}
	
	/**
	 * 进入个人信息修改界面
	 * @author zxit
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * */
	public String updateSelfInput() throws IllegalAccessException, InvocationTargetException {
		User tu = (User)ActionContext.getContext().getSession().get("loginUser");
		BeanUtils.copyProperties(user, tu);
		return SUCCESS;
	}
	
	/**
	 * 修改个人信息修改界面
	 * @author zxit
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * */
	public String updateSelf() {
		User tu = userService.load(user.getId());
		tu.setEmail(user.getEmail());
		tu.setNickname(user.getNickname());
		userService.update(tu);
		ActionContext.getContext().getSession().put("loginUser",tu);
		ActionUtil.setUrl("/later_user_showSelf.action");
		return ActionUtil.REDIRECT;
	}
	
	/**
	 * 个人信息展示
	 * @author zxit
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * */
	public String showSelf() throws IllegalAccessException, InvocationTargetException {
		User tu = (User)ActionContext.getContext().getSession().get("loginUser");
		BeanUtils.copyProperties(user, tu);
		return SUCCESS;
	}
	
}
